macheteYeti / [Fit AF] Extract Macros

// ==UserScript==
// @name         [Fit AF] Extract Macros
// @namespace    Fit AF
// @include https://recipes.sparkpeople.com/*
// @require http://code.jquery.com/jquery-3.4.1.min.js
// @resource customCSS https://code.jquery.com/ui/1.12.1/themes/ui-lightness/jquery-ui.css
// @require https://code.jquery.com/ui/1.12.0/jquery-ui.min.js
// @grant       GM_addStyle
// @license		MIT	
// @grant       GM_getResourceText
// @author       macheteYeti
// @description  Extracts calculated macros from Spark Nutrition
// ==/UserScript==

$(document).ready(function(){

	var newCSS = GM_getResourceText ("customCSS");
	GM_addStyle (newCSS);
	GM_addStyle( `
	
		#site_center3,#site_center2{margin-left:0 !important}
	` );
	
	$('#inpagesearch-1').focus();
	var urlParams = new URLSearchParams(window.location.search);
	var haveParams=0;
	if(typeof urlParams.get('meal')!=typeof undefined&&urlParams.get('meal')!=null&&urlParams.get('meal').length>1){
	
		$('header,#nav5_ad_bg_flex,#top_calc_w,#share1_w,footer').hide();
		$('#wizard .content').css('height','auto !important');
		$('.actions.clearfix').prependTo($('#wizard')).css('text-align','center');
		$('.step1_intro').insertAfter($('#calc_w'));
		var form='<form id="pushMacros" style="padding: 20px 10px;background: #fff;"><h3> Fit AF Import</h3><fieldset style="display: flex;justify-content: space-around;margin-top: 5px;border: 0;"><input id="faSize" placeholder="Size" value="'+urlParams.get('size')+'" style="width:30px"><input id="faMeal" value="'+urlParams.get('meal')+'" placeholder="Meal Lookup"></fieldset><br><button id="pushMacs" class="button big orange calc_btn">Save</button></form>';
		haveParams=1;
	}
	else var form='<form id="pushMacros" style="padding: 20px 10px;background: #fff;"><h3> Fit AF Import</h3><fieldset style="display: flex;justify-content: space-around;margin-top: 5px;border: 0;"><input id="faSize" placeholder="Size" style="width:30px"><input id="faMeal" placeholder="Meal Lookup"></fieldset></form>';
	
	$(document).keyup(function(e){if(e.which===27||e.keyCode===27){$('.lightbox_close_btn').trigger('click');}});
	$('#calc_w a').click(function(){
	
		setTimeout(function(){
		
		$('body').prepend($('<textarea id="macros" style="top:0;height:1px;width:1px;right:0;position:absolute"></textarea>'));
		
		var macs=$('#pop_frame').contents().find('#facts_ul').text().trim();
		macs+="\n"+$('#pop_frame').contents().find('#vitamins_ul').text().trimLeft();
		macs=macs.split("\n");
		for(i in macs){
		
			macs[i]=macs[i].trim().trimStart().replace(" %","").replace(" g","").replace(" mg","").replace('-6','-six').replace('-12','-twelve');
			macs[i]=macs[i].slice(0,macs[i].lastIndexOf(' '))+macs[i].slice(macs[i].lastIndexOf(' ')+1);
			macs[i]=macs[i].replaceAll(' ','_');
		}
		macs=macs.join("\n");
		$('#macros').text(macs.trim().trimStart());
		$('#macros').focus();
		$('#macros').select();
		document.execCommand('copy');
		
		if($('#pushMacros').length==0)$(form).insertAfter($('#calc_w'));
		else{
		
			if(!haveParams)$('#pushMacros input').empty();
			if($('#pushMacros').css('display')=='none')$('#pushMacros').fadeIn();
		}
		/* $([document.documentElement,document.body]).animate({ scrollTop: $("#pushMacros").offset().top },750); */
		$.ajax({
		
			url:'https://fitaf570.com/getMacs.php',
			type:'POST',
			crossDomain:true,
			cache:false,
			dataType:'json',
			data:{type:'meals'},
			success:function(d){
			
				$('#faMeal').autocomplete({
				
					source:d,
					width:500,
					delay:100,
					autoFocus:true,
					select:function(e,ui){
					
						e.preventDefault();
						pushMacs(ui.item.value);
					}
				});
			}
		});
		},1000);
		
		function pushMacs(meal){
		
			$.ajax({
			
				url:'https://fitaf570.com/getMacs.php',
				type:'POST',
				crossDomain:true,
				cache:false,
				dataType:'json',
				data:{type:'macs',size:$('#faSize').val(),meal:meal,macs:$('#macros').text()},
				success:function(d){
				
					if(d.success==true)$('#pushMacros').fadeOut();
					else $('#pushMacros').addClass('ui-state-error').effect('shake');
				}
			});
		}
		$(document).on('click','#pushMacs',function(e){
		
			e.preventDefault();
			
			pushMacs($('#faMeal').val());
		});
	});
});